<template>
  <div class="watch-demo3">
    <h1>情况三:监视【reactive】定义的【对象类型】数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">修改整个人</button>
  </div>
</template>

<script lang="ts" setup name="WatchDemo3">
import { reactive, watch } from "vue";
let person = reactive({
  name: "张三",
  age: 18,
});
function changeName() {
  //修改对象中的某个属性，监听到的新值和旧值相等
  person.name += "~";
}
function changeAge() {
  //修改对象中的某个属性，监听到的新值和旧值相等
  person.age += 1;
}
function changePerson() {
  //修改整个对象，监听到的新值和旧值相等
  Object.assign(person, { name: "赵六", age: 60 });
}
//默认开启了深度监听
watch(person, (newValue, oldValue) => {
  console.log(`person发生了变化`, newValue, oldValue);
});
</script>

<style>
.watch-demo3 {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
</style>